package com.yongcheon.sms_manager.ui.preference;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.phone_spsm.PhoneSpamData;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.data.sms_spam.SMSData;
import com.yongcheon.sms_manager.data.sms_spam.SpamData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.ui.dialogs.SpamRegistDlg;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.SMSVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-10.
 */
public class SpamNumLongClickListener implements View.OnLongClickListener {
    View view;
    SpamNumVO spamNumVO;
    SpamNumCursorAdapter spamNumCursorAdapter;

    public SpamNumLongClickListener(View view, SpamNumVO spamNumVO, SpamNumCursorAdapter spamNumCursorAdapter){
        super();
        this.view = view;
        this.spamNumVO = spamNumVO;
        this.spamNumCursorAdapter = spamNumCursorAdapter;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] MENU_LIST = {v.getContext().getResources().getString(R.string.edit), v.getContext().getResources().getString(R.string.delete_restore_sms)};
        AlertDialog dialog = new AlertDialog.Builder(v.getContext())
                .setTitle(spamNumVO.getSubject())
                .setItems(MENU_LIST, new SpamDialogOnClickListener(v.getContext()))
                .create();

        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return false;
    }

    public class SpamDialogOnClickListener implements  DialogInterface.OnClickListener {
        private Context context;
        private SMSData smsData;
        private SpamData spamData;
        private SpamNumberData spamNumData;
        private SQLiteDatabase database;

        public SpamDialogOnClickListener(Context context) {
            super();
            this.context = context;
            smsData = new SMSData();
            spamData = new SpamData(context);
            spamNumData = new SpamNumberData(context);
            database = SpamDBManager.getDatabase(context);
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0 :
                    SpamRegistDlg.showSpamRegistDlg(context, view, SpamNumLongClickListener.this.spamNumVO, this, false);
                    break;
                case 1 :
                    if(DefaultAppUtil.isWritable(context)) {
                        unRegistSpam(database, spamData, smsData, spamNumData);
                    }
                    PhoneSpamData.unRegistSpam(SpamNumLongClickListener.this.spamNumVO, context);
                    break;
            }
        }

        public void editSpam(SpamNumVO spamNumVO){
            spamNumData.update(SpamNumLongClickListener.this.spamNumVO, context, database);
            spamNumCursorAdapter.getCursor().requery();
        }

        public void unRegistSpam(SQLiteDatabase database, SpamData spamData, SMSData smsData, SpamNumberData spamNumData){
            try {
                database.beginTransaction();
                String address = SpamNumLongClickListener.this.spamNumVO.getAddress();
                ArrayList<SMSVO> smsList = spamData.select(address, context, database);

                for (int i = 0; i < smsList.size(); i++) {
                    spamData.delete(smsList.get(i).get_Id(), context, database);
                }

                for (int i = 0; i < smsList.size(); i++) {
                    smsData.insert(smsList.get(i), context, null);
                }

                spamNumData.delete(address, context, database);

                spamNumCursorAdapter.getCursor().requery();
                database.setTransactionSuccessful();
            }catch(Exception e){
                database.endTransaction();
                e.printStackTrace();
            }finally {
                database.endTransaction();
            }
        }
    }
}
